package com.oldroid.apps.getit.ui.adapter;

import java.sql.Date;

import com.oldroid.apps.getit.R;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class SimpleDownloadsAdapter extends android.support.v4.widget.SimpleCursorAdapter {
	private Context mContext;
	private String filename;

	@SuppressWarnings("deprecation")
	public SimpleDownloadsAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
		super(context, layout, c, from, to);
		mContext = context;
	}
	

	@Override
	public void bindView(View arg0, Context arg1, Cursor arg2) {
		// TODO Auto-generated method stub
		super.bindView(arg0, arg1, arg2);
	}


	@Override
	public void setViewText(TextView v, String text) {
		super.setViewText(v, convertText(v, text));
	}
	
	

	private String convertText(TextView v, String text) {
		switch (v.getId()) {
		// case R.id.downloads_filename:
		// if (text.contains(".")) {
		// String[] temp = text.split(".");
		// return temp[0];
		// } else {
		// return text;
		// }
		case R.id.downloads_filename:
			filename = text;
			return text;
		case R.id.downloads_fileuri:
			int end = text.indexOf(filename);
			return text.substring(0, end);
		case R.id.downloads_filesize:
			double MEGABYTE = 1024.0 * 1024.0;
			double KILOBYTE = 1024.0;
			if (Double.parseDouble(text) > MEGABYTE) {
				return String.format("%2.2f MB", Integer.parseInt(text) / MEGABYTE);
			} else if (Double.parseDouble(text) >= 1000) {
				return String.format("%2.2f KB", Integer.parseInt(text) / KILOBYTE);
			} else
				return String.format("%d bytes", Integer.parseInt(text));
		case R.id.downloads_created:
			Date date = new Date(Long.parseLong(text));
			return "" + (date.getMonth() + 1) + "/" + (date.getDate()) + "/" + (date.getYear() + 1900);
		default:
			return text;
		}
	}

}
